# vim: ts=4:sw=4:syntax=perl

# Take all the args and update portal ... and then
# redirect to page in the LP parameter.
use gri_secmgr qw(get_user_name redirect_header);
use gri_user_alert_config;
use File::Basename;
use xml_helper;
use Data::Dumper;
use gri2_requests;

sub mm_form_portal_access_request_process($) {
my $_aa=new xml_helper();
my $portal=shift;
# my $portal=$::PARAM{portal};

	# We get details of the portal and if it has one or more
	# voters then we raise a request, otherwise we generate an email as
	# per the older mechanisms.

	my $url_prefix=$::GRI_FRONTEND->get_url_prefix();
	my $user=$::PARAM{user};
	my $justification=$::PARAM{justification};
	$justification="" if !defined($justification);
	my $pstr=hex_to_string($portal);
	my $ustr=hex_to_string($user);
	my $dd=$::GRI_FRONTEND->get_portal_details($pstr);
	if(defined($dd->{error})) {
		my $serror=$dd->{error};
		my $error="Failed to raise access to portal request.";
		my $link="$url_prefix$::ENV{SCRIPT_NAME}/show_generic_error?serror=".string_to_hex($serror).",error=".string_to_hex($error).",previouspage=";
		redirect_header(path => $link);
		exit;
	}
	my $req_dir=$::GRI_FRONTEND->get_directory("requests");
	if(!defined($req_dir)) {
		my $error="Failed to find 'requests' directory.";
		my $serror="";
		my $link="$url_prefix$::ENV{SCRIPT_NAME}/show_generic_error?serror=".string_to_hex($serror).",error=".string_to_hex($error).",previouspage=";
		redirect_header(path => $link);
		exit;
	}
	# Ok - need to generate some hmtl so start the page..
	load_macro('mm_start_page');
	load_macro('mm_header');
	mm_start_page();
	my $s=mm_header(undef,undef,1);
	my $link=my $l=link_to_str("stdlink","here","_","");
	if(! @{$dd->{voters}}) {
		load_macro('mm_send_email2');
		load_macro('mm_back_to_portal_list');
		# No voters, so send owners an email (if any) otherwise a
		# generic one ...
		my $user_s=hex_to_string($user);
		my $portal_s=hex_to_string($portal);
		
		my $rdb=new gri2_requests("$req_dir/requests.db");
		if(!defined($rdb)) {
			my ($rc,$str)=gri2_requests::error();
			return html_error("Failed to open requests database: $str",1);
		}
		my $err=$rdb->register_portal_access_request({
			user => $user_s, portal => $portal_s, 
			text => $justification, frontend => $::GRI_FRONTEND,
		});
		if(defined($err->{error})) {
			return html_error($err->{error},1);
		}
		if(defined($err->{notes})) {
			return "<P>$err->{notes} - ID $err->{id}. Click $link to return to main page.";
		}
		$justification =~ s/^\s+//;
		$justification =~ s/\s+$//;
		if(@{$dd->{owners}}) {
			my $email=$::GRI_FRONTEND->get_user_email_address($ustr);
			if(!defined($email) || !length($email)) {
				return "<P> Failed to ascertain email address for '$ustr'. Click $link to return to main page.";
			}
			my $txt="<font style='font-weight: bold;'>You are receiving this email since you are classed as an owner of the portal '$pstr'. If you wish to grant access to '$ustr' [$email] please use the link below.</font>";

			my $info={
				text=>$txt,
				owners=>$dd->{owners},
			};
			if(length($justification)) {
				$info->{justification}=$justification;
			}
			$s.=mm_send_email2($portal,$user,$info);
			$s.=mm_back_to_portal_list($portal);

			if($err->{id}) {
				$s.="<P>Your ID has been generated request ID $err->{id}.";
			}
			return $s;
		} else {
			# If no owneres or voters then send out the old-style email ... but
			# still register as a request.
			$s.=mm_send_email2($portal,$user,{justification=>$justification});
			if($err->{id}) {
				$s.="<P>Your ID has been generated request ID $err->{id}.<P>";
			}
			$s.=mm_back_to_portal_list($portal);
			return $s;
		}
	}

	# If there is any justification text incl
	my $rdb=new gri2_requests("$req_dir/requests.db");
	if(!defined($rdb)) {
		my ($rc,$str)=gri2_requests::error();
		return html_error("Failed to open requests database: $str",1);
	}

	my $user_s=hex_to_string($user);
	my $portal_s=hex_to_string($portal);
	
	my $err=$rdb->register_portal_access_request({
		user => $user_s, portal => $portal_s,
		text => $justification, frontend => $::GRI_FRONTEND,
	});
	if(defined($err->{error})) {
		return html_error($err->{error},1);
	}

	# Ok the request has been created - so show the user the ID of the request and
	# indicate they should keep it as a reference.

	if(defined($err->{notes})) {
		return "<P>$err->{notes} - ID $err->{id}. Click $link to return to main page.";
	}
	return "<P>Your request has been registered - ID $err->{id}. Click $link to return to main page.";
}

1;
